#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Handles all console input/output

Contains both the code to grab and parse command-line arguments and code to 
display and grab user input.
"""

from __future__ import print_function
import argparse

import __main__ as meta

class CommandIo(object):
    """A class to parse command-line arguments"""
    def __init__(self):
        """Creates the command line parser."""
        self._parser = None
        self._create_parser()
        return

    def _create_parser(self):
        """Adds options for the parser."""
        ## Creating the parser
        self._parser = argparse.ArgumentParser(
            description=meta.__doc__,
            add_help=True,
            prog=meta.__program__,
            formatter_class=argparse.RawTextHelpFormatter)
        
        ## Positional (mandatory) arguments
        self._parser.add_argument(
            "input_path",
            nargs='?',
            type=str,
            help="The path to the file to test.")
        
        ## Optional arguments
        self._parser.add_argument(
            "-v", "--version",
            action="version",
            version="Version:  " + meta.__version__ +
                '\nReleased: ' + meta.__release__,
            help="Display the version and release date.")
        self._parser.add_argument(
            "-l", "--language",
            action="store",
            nargs="?",
            const=None,
            default='spanish',
            type=str,
            choices=['spanish', 'english'],
            help='The language to test',
            dest='language')
        return

    def parse(self):
        """Runs the command line parser."""
        return self._parser.parse_args()
    
    def run_args(self, *args):
        """Runs an arbitary set of arguments"""
        return self._parser.parse_args(args)


class Prompter(object):
    """Prompts the user for words when starting"""
    def __init__(self):
        self.meta_mark = '@'
        self.phrases = {
            '0': 'Display the word',
            '1': 'Begin cleanup'}
        return
    
    def ask(self, words_list):
        """Begins asking each word in the list until they run out"""
        try:
            for (question, answer) in words_list:
                is_correct = False
                while not is_correct:
                    print('>', question[0])
                    user_answer = raw_input('? ')
                        
                    if user_answer in answer:
                        is_correct = True
                        
                    if user_answer in self._get_allowed_meta():
                        self._apply_action(user_answer)
                    
                    if user_answer in answer:
                        is_correct = True
                    print('')
        except(KeyboardInterrupt, EOFError):
            pass
        return
    
    def _get_allowed_meta(self):
        """Return any allowed meta keywords"""
        return [self.meta_mark + str(k) for k in self.phrases.keys()]
    
    def _apply_action(self, answer):
        pass
            
    
        
    


